package com.arun.servceImpl;

import com.arun.dao.ssm.YMDDao;
import com.arun.entity.CUXDHSBatteryINFO;
import com.arun.entity.Data;
import com.arun.service.YMDDaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class YMDDaoServiceImpl implements YMDDaoService {
    @Autowired
    private YMDDao ymdDao;

    @Override
    @Transactional
    public int saveCUXDHSBatteryINFO(CUXDHSBatteryINFO cuxdhsBatteryINFO) {
        int rtn;
        rtn = ymdDao.insertCUXDHSBatteryINFO(cuxdhsBatteryINFO);
        //errMethod();
        return rtn;
    }

    @Override
    @Transactional
    public int saveCUXDHSBatteryINFOList(List<CUXDHSBatteryINFO> cuxdhsBatteryINFOList) {
        int rtn;
        rtn = ymdDao.insertCUXDHSBatteryINFOs(cuxdhsBatteryINFOList);
        //errMethod();
        return rtn;
    }

    /**
     * @desc 将Data保存到CUXDHSBatteryINFO
     * @param data
     * @return 返回数据库更新的数量
     */
    public int saveCUXDHSBatteryINFOFromData(Data data){
        int rtn;
        List<CUXDHSBatteryINFO> list = transferDataTOcuxDHSBatteryINFO(data);
        rtn = saveCUXDHSBatteryINFOList(list);
        return rtn;
    }
    /**
     * @param data
     * @desc Data json 转换为实体类 CUXDHSBatteryINFO
     */
    public List<CUXDHSBatteryINFO> transferDataTOcuxDHSBatteryINFO(Data data){
        List<CUXDHSBatteryINFO> list = new ArrayList<>();
        List<Data.DataDTO> dataDTOS = data.getData();

        Date date = new Date();

        for (int i = 0; i < dataDTOS.size(); i++) {
            String BatCode =dataDTOS.get(i).getBatCode();
            List<Data.DataDTO.DetailsDTO> details = dataDTOS.get(i).getDetails();
            for (int i1 = 0; i1 < details.size(); i1++) {
                CUXDHSBatteryINFO cuxdhsBatteryINFO = new CUXDHSBatteryINFO();
                cuxdhsBatteryINFO.setBatCode(BatCode);
                cuxdhsBatteryINFO.setWoNo(details.get(i1).getWoNo());
                cuxdhsBatteryINFO.setWoName(details.get(i1).getWoName());
                cuxdhsBatteryINFO.setEndV(details.get(i1).getEndV());
                cuxdhsBatteryINFO.setEndI(details.get(i1).getEndI());
                cuxdhsBatteryINFO.setEndC(details.get(i1).getEndC());
                cuxdhsBatteryINFO.setAddTime(details.get(i1).getAddTime());
                cuxdhsBatteryINFO.setDateTime(date);
                list.add(cuxdhsBatteryINFO);
            }

        }

        return list;
    }

    /**
     *
     * @param cuxdhsBatteryINFO
     * @desc 实体类 CUXDHSBatteryINFO 转换为 Data json
     */
    public Data transferCuxDHSBatteryINFOToData(CUXDHSBatteryINFO cuxdhsBatteryINFO){
        Data data = new Data();
        Map<String, Data.DataDTO.DetailsDTO> map = new HashMap<>();
        Data.DataDTO dataDTO = data.getData().get(0);
        dataDTO.setBatCode(cuxdhsBatteryINFO.getBatCode());
        List<Data.DataDTO.DetailsDTO> details = dataDTO.getDetails();
        details.get(0).setWoNo(cuxdhsBatteryINFO.getWoNo());
        details.get(0).setWoName(cuxdhsBatteryINFO.getWoName());
        details.get(0).setEndC(cuxdhsBatteryINFO.getEndC());
        details.get(0).setEndV(cuxdhsBatteryINFO.getEndV());
        details.get(0).setEndI(cuxdhsBatteryINFO.getEndI());
        details.get(0).setAddTime(cuxdhsBatteryINFO.getAddTime());

        return data;
    }
    // @TODO
    public Data transferCuxDHSBatteryINFOsToData(List<CUXDHSBatteryINFO> list){
        Data data = new Data();
        for (int i = 0; i < list.size(); i++) {
            transferCuxDHSBatteryINFOToData(list.get(i));
        }
        return data;
    }

    /**
     * 测试@Transactional
     */
    private void errMethod() {
        System.out.println("error");
        throw new RuntimeException("runtime");
    }
}
